/*
 *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU Lesser Public License v2.1
 *  which accompanies this distribution, and is available at
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 *  Contributors:
 *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Client, FileChooser and WebDAV
 */
package de.ipk_gatersleben.bit.bi.edal.publication;

import java.awt.Dimension;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel.AuthorsPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel.LanguagePanel;
import de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel.PublisherPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel.SubjectPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.NonFreeTextPanelMouseAdapter;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.NonFreeTextPanelMouseAdapter.PanelType;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.TabKeyListener;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.TextAreaCheckFocusListener;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.TextAreaValueChangedFocusListener;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.UpdateUIListener;

public class PublicationMainPanel extends JPanel {

	private static final long serialVersionUID = -894068063132047509L;

	public static final String DEFAULT_UPLOAD_PATH_STRING = PropertyLoader.props
			.getProperty("DEFAULT_UPLOAD_PATH_STRING");
	public static final String DEFAULT_TITLE_STRING = PropertyLoader.props
			.getProperty("DEFAULT_TITLE_STRING");
	public static final String DEFAULT_LANGUAGE_STRING = PropertyLoader.props
			.getProperty("DEFAULT_LANGUAGE_STRING");
	public static final String DEFAULT_PUBLISHER_STRING = PropertyLoader.props
			.getProperty("DEFAULT_PUBLISHER_STRING");
	public static final String DEFAULT_DESCRIPTION_STRING = PropertyLoader.props
			.getProperty("DEFAULT_DESCRIPTION_STRING");
	public static final String DEFAULT_AUTHORS_STRING = PropertyLoader.props
			.getProperty("DEFAULT_AUTHORS_STRING");
	public static final String DEFAULT_SUBJECTS_STRING = PropertyLoader.props
			.getProperty("DEFAULT_SUBJECTS_STRING");
	public static final String DEFAULT_EMBARGO_STRING = PropertyLoader.props
			.getProperty("DEFAULT_EMBARGO_STRING");

	public static AttributeTextArea uploadPathField = new AttributeTextArea(
			DEFAULT_UPLOAD_PATH_STRING, false, true);
	public static AttributeTextArea titleField = new AttributeTextArea(
			DEFAULT_TITLE_STRING, true, true);
	public static AttributeTextArea descriptionField = new AttributeTextArea(
			DEFAULT_DESCRIPTION_STRING, true, false);
	public static AttributeTextArea authorsField = new AttributeTextArea(
			DEFAULT_AUTHORS_STRING, false, true);
	public static AttributeTextArea subjectsField = new AttributeTextArea(
			DEFAULT_SUBJECTS_STRING, false, true);
	public static AttributeTextArea languageField = new AttributeTextArea(
			DEFAULT_LANGUAGE_STRING, false, true);
	public static AttributeTextArea publisherField = new AttributeTextArea(
			DEFAULT_PUBLISHER_STRING, false, true);
	public static AttributeTextArea embargoField = new AttributeTextArea(
			DEFAULT_EMBARGO_STRING, false, true);

	public static AuthorsPanel authorPanel;
	public static LanguagePanel languagePanel;
	public static SubjectPanel subjectPanel;
	public static PublisherPanel publisherPanel;
	public static PublicationButtonLinePanel publicationButtonLinePanel;

	private static NonFreeTextPanelMouseAdapter openAuthorPanelListener;
	private static NonFreeTextPanelMouseAdapter openLanguagePanelListener;
	private static NonFreeTextPanelMouseAdapter openUploadPanelListener;
	private static NonFreeTextPanelMouseAdapter openSubjectPanelListener;
	private static NonFreeTextPanelMouseAdapter openPublisherPanelListener;
	private static NonFreeTextPanelMouseAdapter openEmbargoPanelListener;

	private static JPanel mainPanel;

	public static int UPLOAD_PANEL_COMPONENT_NUMBER = 0;
	public static int TITLE_PANEL_COMPONENT_NUMBER = 1;
	public static int AUTHOR_PANEL_COMPONENT_NUMBER = 2;
	public static int DESCRIPTION_PANEL_COMPONENT_NUMBER = 3;
	public static int SUBJECTS_PANEL_COMPONENT_NUMBER = 4;
	public static int LANGUAGE_PANEL_COMPONENT_NUMBER = 5;
	public static int PUBLISHER_PANEL_COMPONENT_NUMBER = 6;
	public static int EMBARGO_DATE_COMPONENT_NUMBER = 7;
	public static int BUTTON_LINE_PANEL_COMPONENT_NUMBER = 8;

	static {

		authorPanel = new AuthorsPanel();
		languagePanel = new LanguagePanel();
		publisherPanel = new PublisherPanel();
		subjectPanel = new SubjectPanel();

		openAuthorPanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.AUTHOR_PANEL, AUTHOR_PANEL_COMPONENT_NUMBER);
		openLanguagePanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.LANGUAGE_PANEL, LANGUAGE_PANEL_COMPONENT_NUMBER);
		openUploadPanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.UPLOAD_PANEL, -1);
		openSubjectPanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.SUBJECT_PANEL, SUBJECTS_PANEL_COMPONENT_NUMBER);
		openPublisherPanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.PUBLISHER_PANEL, PUBLISHER_PANEL_COMPONENT_NUMBER);
		openEmbargoPanelListener = new NonFreeTextPanelMouseAdapter(
				PanelType.EMBARGO_PANEL, EMBARGO_DATE_COMPONENT_NUMBER);

	}

	public PublicationMainPanel() {

		/* set ValueChangedListener */

		titleField.addFocusListener(new TextAreaValueChangedFocusListener(
				PropertyLoader.TITLE_LABEL, titleField, DEFAULT_TITLE_STRING));

		descriptionField
				.addFocusListener(new TextAreaValueChangedFocusListener(
						PropertyLoader.DESCRIPTION_LABEL, descriptionField,
						DEFAULT_DESCRIPTION_STRING));

		publisherField.addFocusListener(new TextAreaCheckFocusListener(
				PropertyLoader.PUBLISHER_LABEL, publisherField,
				DEFAULT_PUBLISHER_STRING));

		/* end ValueChangedListener */

		/* set other Listeners */

		authorsField.addMouseListener(openAuthorPanelListener);
		languageField.addMouseListener(openLanguagePanelListener);
		uploadPathField.addMouseListener(openUploadPanelListener);
		subjectsField.addMouseListener(openSubjectPanelListener);
		publisherField.addMouseListener(openPublisherPanelListener);
		embargoField.addMouseListener(openEmbargoPanelListener);

		titleField.addFocusListener(new UpdateUIListener());
		publisherField.addFocusListener(new UpdateUIListener());

		titleField.addKeyListener(new TabKeyListener(descriptionField));
		descriptionField.addKeyListener(new TabKeyListener(publisherField));

		/* end other Listeners */

		AttributeTablePanel descriptionPanel = new AttributeTablePanel(
				PropertyLoader.DESCRIPTION_LABEL, descriptionField,
				PropertyLoader.DESCRIPTION_PANEL_HEIGHT);

		AttributeTablePanel titleAttributePanel = new AttributeTablePanel(
				PropertyLoader.TITLE_LABEL, titleField,
				PropertyLoader.THREE_TIMES_SIZE);

		AttributeTablePanel uploadAttributePanel = new AttributeTablePanel(
				PropertyLoader.UPLOADPATH_LABEL, uploadPathField,
				PropertyLoader.DEFAULT_FONT_SIZE);

		AttributeTablePanel authorsAttributePanel = new AttributeTablePanel(
				PropertyLoader.AUTHORS_LABEL, authorsField,
				PropertyLoader.THREE_TIMES_SIZE);

		AttributeTablePanel subjectsAttributePanel = new AttributeTablePanel(
				PropertyLoader.SUBJECTS_LABEL, subjectsField,
				PropertyLoader.THREE_TIMES_SIZE);

		AttributeTablePanel languageAttributePanel = new AttributeTablePanel(
				PropertyLoader.LANGUAGE_LABEL, languageField,
				PropertyLoader.DEFAULT_FONT_SIZE);

		AttributeTablePanel publisherAttributePanel = new AttributeTablePanel(
				PropertyLoader.PUBLISHER_LABEL, publisherField,
				PropertyLoader.PUBLISHER_PANEL_HEIGHT);

		AttributeTablePanel embargoAttributePanel = new AttributeTablePanel(
				PropertyLoader.EMBARGO_LABEL, embargoField,
				PropertyLoader.THREE_TIMES_SIZE);

		publicationButtonLinePanel = new PublicationButtonLinePanel();

		mainPanel = new JPanel();

		BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);

		mainPanel.setLayout(boxLayout);

		mainPanel.add(uploadAttributePanel, UPLOAD_PANEL_COMPONENT_NUMBER);
		mainPanel.add(titleAttributePanel, TITLE_PANEL_COMPONENT_NUMBER);
		mainPanel.add(authorsAttributePanel, AUTHOR_PANEL_COMPONENT_NUMBER);
		mainPanel.add(descriptionPanel, DESCRIPTION_PANEL_COMPONENT_NUMBER);
		mainPanel.add(subjectsAttributePanel, SUBJECTS_PANEL_COMPONENT_NUMBER);
		mainPanel.add(languageAttributePanel, LANGUAGE_PANEL_COMPONENT_NUMBER);
		mainPanel
				.add(publisherAttributePanel, PUBLISHER_PANEL_COMPONENT_NUMBER);
		mainPanel.add(embargoAttributePanel, EMBARGO_DATE_COMPONENT_NUMBER);
		mainPanel.add(publicationButtonLinePanel,
				BUTTON_LINE_PANEL_COMPONENT_NUMBER);
		mainPanel.setBackground(PropertyLoader.HEADER_FOOTER_COLOR);

		JScrollPane scrollableMainPane = new JScrollPane(mainPanel,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		scrollableMainPane.setPreferredSize(new Dimension(400, 400));
		scrollableMainPane.setBorder(BorderFactory.createEmptyBorder());
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(scrollableMainPane);

	}

	public static JPanel getMainPanel() {
		return PublicationMainPanel.mainPanel;
	}

	/**
	 * Block all textfields except the Authorsfield
	 * 
	 */
	public static void blockForAuthorsField() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(false);
		deactivateFreeTextFields();
		removeLanguageFieldListener();
		removeUploadPanelListener();
		removeSubjectFieldListener();
		removePublisherPanelListener();

	}

	private static void removeAuthorPanelListener() {
		for (MouseListener iterable_element : authorsField.getMouseListeners()) {
			if (iterable_element.equals(openAuthorPanelListener)) {
				authorsField.removeMouseListener(openAuthorPanelListener);
				break;
			}
		}
	}

	private static void removeLanguageFieldListener() {
		for (MouseListener iterable_element : languageField.getMouseListeners()) {
			if (iterable_element.equals(openLanguagePanelListener)) {
				languageField.removeMouseListener(openLanguagePanelListener);
				break;
			}
		}
	}

	private static void removeUploadPanelListener() {
		for (MouseListener iterable_element : uploadPathField
				.getMouseListeners()) {
			if (iterable_element.equals(openUploadPanelListener)) {
				uploadPathField.removeMouseListener(openUploadPanelListener);
				break;
			}
		}
	}

	private static void removePublisherPanelListener() {
		for (MouseListener iterable_element : publisherField
				.getMouseListeners()) {
			if (iterable_element.equals(openPublisherPanelListener)) {
				publisherField.removeMouseListener(openPublisherPanelListener);
				break;
			}
		}
	}

	private static void removeSubjectFieldListener() {
		for (MouseListener iterable_element : subjectsField.getMouseListeners()) {
			if (iterable_element.equals(openSubjectPanelListener)) {
				subjectsField.removeMouseListener(openSubjectPanelListener);
				break;
			}
		}
	}

	public static void releaseAllBlockedFields() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(true);

		titleField.setEditable(true);
		descriptionField.setEditable(true);

		titleField.setFocusable(true);
		descriptionField.setFocusable(true);
		uploadPathField.setFocusable(true);

		boolean hasLanguageListener = false;

		for (MouseListener iterable_element : languageField.getMouseListeners()) {
			if (iterable_element.equals(openLanguagePanelListener)) {
				hasLanguageListener = true;
			}
		}

		if (!hasLanguageListener) {
			languageField.addMouseListener(openLanguagePanelListener);
		}

		boolean hasAuthorListener = false;

		for (MouseListener iterable_element : authorsField.getMouseListeners()) {
			if (iterable_element.equals(openAuthorPanelListener)) {
				hasAuthorListener = true;
			}
		}

		if (!hasAuthorListener) {
			authorsField.addMouseListener(openAuthorPanelListener);
		}

		boolean hasUploadPathListener = false;

		for (MouseListener iterable_element : uploadPathField
				.getMouseListeners()) {
			if (iterable_element.equals(openUploadPanelListener)) {
				hasUploadPathListener = true;
			}
		}

		if (!hasUploadPathListener) {
			uploadPathField.addMouseListener(openUploadPanelListener);
		}

		boolean hasSubjectListener = false;

		for (MouseListener iterable_element : subjectsField.getMouseListeners()) {
			if (iterable_element.equals(openSubjectPanelListener)) {
				hasSubjectListener = true;
			}
		}

		if (!hasSubjectListener) {
			subjectsField.addMouseListener(openSubjectPanelListener);
		}

		boolean hasPublisherListener = false;

		for (MouseListener iterable_element : publisherField
				.getMouseListeners()) {
			if (iterable_element.equals(openPublisherPanelListener)) {
				hasPublisherListener = true;
			}
		}

		if (!hasPublisherListener) {
			publisherField.addMouseListener(openPublisherPanelListener);
		}

	}

	/**
	 * Block all fields except the LanguageField
	 */
	public static void blockForLanguageField() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(false);
		deactivateFreeTextFields();
		removeAuthorPanelListener();
		removeUploadPanelListener();
		removeSubjectFieldListener();
		removePublisherPanelListener();
	}

	public static void blockForSubjectsField() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(false);
		deactivateFreeTextFields();
		removeLanguageFieldListener();
		removeUploadPanelListener();
		removeAuthorPanelListener();
		removePublisherPanelListener();
	}

	private static void deactivateFreeTextFields() {
		titleField.setEditable(false);
		descriptionField.setEditable(false);
		titleField.setFocusable(false);
		descriptionField.setFocusable(false);
	}

	public static void blockForPublisherField() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(false);
		deactivateFreeTextFields();
		removeAuthorPanelListener();
		removeUploadPanelListener();
		removeLanguageFieldListener();
		removeSubjectFieldListener();
	}

	public static void blockForTextField() {
		publicationButtonLinePanel.getSubmitButton().setEnabled(false);
		removeAuthorPanelListener();
		removeUploadPanelListener();
		removeLanguageFieldListener();
		removeSubjectFieldListener();
	}

	public static void reset() {
		uploadPathField.cleanTextArea();
		titleField.cleanTextArea();
		descriptionField.cleanTextArea();
	}
}
